﻿#include "SpellObject.h"
#include "../MirScenes/GameScene.h"
#include "../MirGraphics/MLibrary.h"
#include "Effect.h"
#include "../Forms/Client.CMain.h"
#include "../MirSounds/SoundManager.h"

using namespace Client::MirGraphics;
using namespace Client::MirScenes;
namespace S = ServerPackets;

namespace Client::MirObjects
{

    ObjectType SpellObject::getRace() const
    {
        return ObjectType::Spell;
    }

    bool SpellObject::getBlocking() const
    {
        return false;
    }

    SpellObject::SpellObject(unsigned int objectID) : MapObject(objectID)
    {
    }

    void SpellObject::Load(S::ObjectSpell *info)
    {
        CurrentLocation = info->Location;
        MapLocation = info->Location;
        GameScene::Scene->MapControl->AddObject(this);
        Spell = info->Spell;
        Direction = info->Direction;
        Repeat = true;

        switch (Spell)
        {
            case Spell::TrapHexagon:
                BodyLibrary = Libraries::Magic;
                DrawFrame = 1390;
                FrameInterval = 100;
                FrameCount = 10;
                Blend = true;
                break;
            case Spell::FireWall:
                BodyLibrary = Libraries::Magic;
                DrawFrame = 1630;
                FrameInterval = 120;
                FrameCount = 6;
                Light = 3;
                Blend = true;
                break;
            case Spell::PoisonCloud:
                BodyLibrary = Libraries::Magic2;
                DrawFrame = 1650;
                FrameInterval = 120;
                FrameCount = 20;
                Light = 3;
                Blend = true;
                break;
            case Spell::DigOutZombie:
                BodyLibrary = static_cast<unsigned short>(Monster::DigOutZombie) < Libraries::Monsters.Count() ? Libraries::Monsters[static_cast<unsigned short>(Monster::DigOutZombie)] : Libraries::Magic;
                DrawFrame = 304 + static_cast<unsigned char>(Direction);
                FrameCount = 0;
                Blend = false;
                break;
            case Spell::Blizzard:
                CurrentLocation->Y = std::max(0, CurrentLocation->Y - 20);
                BodyLibrary = Libraries::Magic2;
                DrawFrame = 1550;
                FrameInterval = 100;
                FrameCount = 30;
                Light = 3;
                Blend = true;
                Repeat = false;
                break;
            case Spell::MeteorStrike:
                Effect *tempVar = new Effect(Libraries::Magic2, 1600, 10, 800, CurrentLocation);
                tempVar->Repeat = true;
                tempVar->RepeatUntil = CMain::Time + 3000;
                MapControl::Effects.push_back(tempVar);
                CurrentLocation->Y = std::max(0, CurrentLocation->Y - 20);
                BodyLibrary = Libraries::Magic2;
                DrawFrame = 1610;
                FrameInterval = 100;
                FrameCount = 30;
                Light = 3;
                Blend = true;
                Repeat = false;
                break;
            case Spell::Rubble:
                if (Direction == (MirDirection)0)
                {
                    BodyLibrary = nullptr;
                }
                else
                {
                    BodyLibrary = Libraries::Effect;
                    DrawFrame = 64 + std::min(4, static_cast<int>(Direction - 1));
                    FrameCount = 1;
                    FrameInterval = 10000;
                }
                break;
            case Spell::Reincarnation:
                BodyLibrary = Libraries::Magic2;
                DrawFrame = 1680;
                FrameInterval = 100;
                FrameCount = 10;
                Light = 1;
                Blend = true;
                Repeat = true;
                break;
            case Spell::ExplosiveTrap:
                BodyLibrary = Libraries::Magic3;
                if (info->Param)
                {
                    DrawFrame = 1570;
                    FrameInterval = 100;
                    FrameCount = 9;
                    Repeat = false;
                    MirSounds::SoundManager::PlaySound(20000 + 124 * 10 + 5); //Boom for all players in range
                }
                else
                {
                    DrawFrame = 1560;
                    FrameInterval = 100;
                    FrameCount = 10;
                    Repeat = true;
                }
                //Light = 1;
                Blend = true;
                break;
            case Spell::Trap:
                BodyLibrary = Libraries::Magic2;
                DrawFrame = 2360;
                FrameInterval = 100;
                FrameCount = 8;
                Blend = true;
                break;
            case Spell::MapLightning:
                Effect tempVar2(Libraries::Dragon, 400 + (CMain::Random->Next(3) * 10), 5, 600, CurrentLocation);
                MapControl::Effects.push_back(&tempVar2);
                MirSounds::SoundManager::PlaySound(8301);
                break;
            case Spell::MapLava:
                Effect *tempVar3 = new Effect(Libraries::Dragon, 440, 20, 1600, CurrentLocation);
                tempVar3->Blend = false;
                MapControl::Effects.push_back(tempVar3);
                Effect tempVar4(Libraries::Dragon, 470, 10, 800, CurrentLocation);
                MapControl::Effects.push_back(&tempVar4);
                MirSounds::SoundManager::PlaySound(8302);
                break;
            case Spell::MapQuake1:
                Effect *tempVar5 = new Effect(Libraries::Monsters[static_cast<unsigned short>(Monster::HellLord)], 27, 12, 1200, CurrentLocation);
                tempVar5->Blend = false;
                MapControl::Effects.push_back(tempVar5);
                MirSounds::SoundManager::PlaySound(8311);
                break;
            case Spell::MapQuake2:
                Effect *tempVar6 = new Effect(Libraries::Monsters[static_cast<unsigned short>(Monster::HellLord)], 39, 13, 1300, CurrentLocation);
                tempVar6->Blend = false;
                MapControl::Effects.push_back(tempVar6);
                MirSounds::SoundManager::PlaySound(8311);
                break;
            case Spell::GeneralMeowMeowThunder:
                Effect *tempVar7 = new Effect(Libraries::Monsters[static_cast<unsigned short>(Monster::GeneralMeowMeow)], 522, 7, 700, CurrentLocation);
                tempVar7->Blend = true;
                MapControl::Effects.push_back(tempVar7);
                MirSounds::SoundManager::PlaySound(8321);
                break;
            case Spell::StoneGolemQuake:
                BodyLibrary = Libraries::Monsters[static_cast<unsigned short>(Monster::StoneGolem)];
                DrawFrame = 368 + static_cast<int>(Direction) * 8;
                FrameInterval = 100;
                FrameCount = 8;
                Light = 0;
                Blend = false;
                Repeat = false;
                MirSounds::SoundManager::PlaySound(8311);
                break;
            case Spell::EarthGolemPile:
                BodyLibrary = Libraries::Monsters[static_cast<unsigned short>(Monster::EarthGolem)];
                DrawFrame = 441;
                FrameInterval = 100;
                FrameCount = 8;
                Light = 0;
                Blend = false;
                Repeat = false;
                MirSounds::SoundManager::PlaySound(8331);
                break;
            case Spell::TreeQueenMassRoots:
                BodyLibrary = Libraries::Monsters[static_cast<unsigned short>(Monster::TreeQueen)];
                DrawFrame = 82;
                FrameInterval = 100;
                FrameCount = 15;
                Blend = false;
                Repeat = false;
                MirSounds::SoundManager::PlaySound(8341);
                Effect *tempVar8 = new Effect(Libraries::Monsters[static_cast<unsigned short>(Monster::TreeQueen)], 97, 14, 1400, CurrentLocation);
                tempVar8->Blend = true;
                MapControl::Effects.push_back(tempVar8);
                break;
            case Spell::TreeQueenGroundRoots:
                BodyLibrary = Libraries::Monsters[static_cast<unsigned short>(Monster::TreeQueen)];
                DrawFrame = 48;
                FrameInterval = 100;
                FrameCount = 9;
                Blend = false;
                Repeat = false;
                MirSounds::SoundManager::PlaySound(8342);
                Effect *tempVar9 = new Effect(Libraries::Monsters[static_cast<unsigned short>(Monster::TreeQueen)], 57, 9, 900, CurrentLocation);
                tempVar9->Blend = true;
                MapControl::Effects.push_back(tempVar9);
                break;
            case Spell::TreeQueenRoot:
                BodyLibrary = Libraries::Monsters[static_cast<unsigned short>(Monster::TreeQueen)];
                DrawFrame = 111;
                FrameInterval = 100;
                FrameCount = 15;
                Blend = false;
                Repeat = false;
                MirSounds::SoundManager::PlaySound(8343);
                break;
            case Spell::TucsonGeneralRock:
                Effect *tempVar10 = new Effect(Libraries::Monsters[static_cast<unsigned short>(Monster::TucsonGeneral)], 552, 20, 2000, CurrentLocation);
                tempVar10->Repeat = false;
                tempVar10->Blend = false;
                MapControl::Effects.push_back(tempVar10);
               // MapControl.Effects.Add(new Effect(Libraries.Monsters[(ushort)Monster.TucsonGeneral], 572, 20, 2000, CurrentLocation) { Repeat = false, Blend = true });
                BodyLibrary = Libraries::Monsters[static_cast<unsigned short>(Monster::TucsonGeneral)];
                DrawFrame = 572;
                FrameInterval = 100;
                FrameCount = 20;
                Light = 1;
                Blend = true;
                Repeat = false;
                break;
            case Spell::Portal:
                BodyLibrary = Libraries::Magic2;
                DrawFrame = 2360;
                FrameInterval = 100;
                FrameCount = 8;
                Blend = true;
                break;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar10' statement was not added since tempVar10 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar9' statement was not added since tempVar9 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar8' statement was not added since tempVar8 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar7' statement was not added since tempVar7 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar6' statement was not added since tempVar6 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar5' statement was not added since tempVar5 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }

        NextMotion = CMain::Time + FrameInterval;
        NextMotion -= NextMotion % 100;
    }

    void SpellObject::Process()
    {
        if (CMain::Time >= NextMotion)
        {
            if (++FrameIndex >= FrameCount && Repeat)
            {
                FrameIndex = 0;
            }

            NextMotion = CMain::Time + FrameInterval;

            switch (Spell)
            {
                case Spell::TucsonGeneralRock:
                    if (FrameIndex == 10)
                    {
                        MirSounds::SoundManager::PlaySound(8351);
                    }
                    break;
            }
        }

        DrawLocation = new Point((CurrentLocation->X - User->Movement->X + MapControl::OffSetX) * MapControl::CellWidth, (CurrentLocation->Y - User->Movement->Y + MapControl::OffSetY) * MapControl::CellHeight);
        DrawLocation->Offset(getGlobalDisplayLocationOffset());
        DrawLocation->Offset(User->OffSetMove);
    }

    void SpellObject::Draw()
    {
        if (FrameIndex >= FrameCount && !Repeat)
        {
            return;
        }
        if (BodyLibrary == nullptr)
        {
            return;
        }

        if (Blend)
        {
            BodyLibrary->DrawBlend(DrawFrame + FrameIndex, DrawLocation, DrawColour, true, 0.8F);
        }
        else
        {
            BodyLibrary->Draw(DrawFrame + FrameIndex, DrawLocation, DrawColour, true);
        }
    }

    bool SpellObject::MouseOver(Point *p)
    {
        return false;
    }

    void SpellObject::DrawBehindEffects(bool effectsEnabled)
    {
    }

    void SpellObject::DrawEffects(bool effectsEnabled)
    {
    }
}
